package com.shop.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.Exception.CategoryException;
import com.shop.entity.Category;
import com.shop.mapper.CateGoryMapper;
import com.shop.service.CategoryService;
import org.omg.PortableInterceptor.INACTIVE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Transactional
@Service
public class ICategoryService implements CategoryService {
    @Autowired
    CateGoryMapper cateGoryMapper;

    @Override
    public int add(Category category) {
        int insert = cateGoryMapper.insert(category);
        return insert;
    }

    @Override
    public Category findById(int id) {
        Category category = cateGoryMapper.findById(id);
        return category;
    }

    @Override
    public void update(Category category) {
        cateGoryMapper.update(category);
    }

    @Override
    public void delete(Integer id) {
        cateGoryMapper.deleteById(id);
    }

    @Override
    public Category selectByname(String cat_name) {
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        wrapper.eq("cat_name",cat_name);
        return cateGoryMapper.selectOne(wrapper);
    }

    @Override
    public List<Category> getListCategory(Integer type) throws CategoryException {
        if (type==null){
            type=3;
        }
        CategoryLevel levelEnum=CategoryLevel.getCategoryLevelEnum(type);
        if (levelEnum==null){
            throw new CategoryException("分类级别参数错误！");
        }
        switch (levelEnum){
            case LEVEL_ONE:
                return getOneLevelList();
            case LEVEL_TWO:
                return getTwoLeveList();
            case LEVEL_THREE:
                return getThreeLevelList();
            default:
                throw new CategoryException("分类级别参数错误！");
        }
    }

    private List<Category> getThreeLevelList() {
        List<Category> oneList=getCategoryByLevel(CategoryLevel.LEVEL_ONE.LEVEL.intValue()-1);
        List<Category> twoList = getCategoryByLevel(CategoryLevel.LEVEL_TWO.LEVEL.intValue()-1);
        List<Category> threeList = getCategoryByLevel(CategoryLevel.LEVEL_THREE.LEVEL.intValue()-1);
        nodeCategoryToParentCategory(threeList,twoList);
        nodeCategoryToParentCategory(twoList,oneList);
        return oneList;
    }

    private List<Category> getTwoLeveList() {
        List<Category> oneList=getCategoryByLevel(CategoryLevel.LEVEL_ONE.LEVEL.intValue()-1);
        //查询二级分类

        List<Category> twoList = getCategoryByLevel(CategoryLevel.LEVEL_TWO.LEVEL.intValue()-1);
        //把二级放在一级下面
        nodeCategoryToParentCategory( twoList,oneList);
        return oneList;
    }

    private void nodeCategoryToParentCategory(List<Category> nodeList, List<Category> parentList) {
        for (Category parent: parentList){
            List<Category> temp=new ArrayList<>();
            for (Category node: nodeList){
                if (node.getCat_pid().intValue()==parent.getCat_id().intValue()){
                    temp.add(node);
                }
            }
            parent.setChildren(temp);
        }
    }

    private List<Category> getOneLevelList() {
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        wrapper.eq("cat_level",0);
        return  cateGoryMapper.selectList(wrapper);
    }
//根据level
    private List<Category> getCategoryByLevel(Integer level){
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        wrapper.eq("cat_level",level);
        List<Category> list = cateGoryMapper.selectList(wrapper);
        return list;
    }

    @Override
    public Page<Category> getPageCategory(Integer type, Integer pagenum, Integer pagesize) throws CategoryException {
        if (type==null){
            type=3;
        }
        CategoryLevel levelEnum=CategoryLevel.getCategoryLevelEnum(type);
        if (levelEnum==null){
            throw new CategoryException("分类级别参数错误！");
        }
        switch (levelEnum){
            case LEVEL_ONE:
                return getOneLevelListPage(pagenum,pagesize);
            case LEVEL_TWO:
                return getTwoLeveListPage(pagenum,pagesize);
            case LEVEL_THREE:
                return getThreeLevelListPage(pagenum,pagesize);
            default:
                throw new CategoryException("分类级别参数错误！");
        }
    }

    private Page<Category> getThreeLevelListPage(Integer pagenum, Integer pagesize) {
        Page<Category> oneLevelListPage = getOneLevelListPage(pagenum, pagesize);
        List<Category> twoList = getCategoryByLevel(1);
        List<Category> threeList = getCategoryByLevel(2);
        nodeCategoryToParentCategory(threeList,twoList);
        nodeCategoryToParentCategory(twoList,oneLevelListPage.getRecords());
        return oneLevelListPage;
    }

    private Page<Category> getTwoLeveListPage(Integer pagenum, Integer pagesize) {
        Page<Category> oneLevelListPage = getOneLevelListPage(pagenum, pagesize);
        List<Category> twoList = getCategoryByLevel(1);
        nodeCategoryToParentCategory(twoList, oneLevelListPage.getRecords());
        return oneLevelListPage;
    }

    private Page<Category> getOneLevelListPage(Integer pagenum, Integer pagesize) {
        Page<Category> page = new Page<>(pagenum, pagesize);
        QueryWrapper<Category> wrapper=new QueryWrapper<>();
        wrapper.eq("cat_level",0);
        return cateGoryMapper.selectPage(page,wrapper);
    }

    public static enum CategoryLevel{
        LEVEL_ONE(1,"一级分类"),
        LEVEL_TWO(2,"二级分类"),
        LEVEL_THREE(3,"三级分类")
        ;
        CategoryLevel(Integer LEVEL, String MESSAGE) {
            this.LEVEL = LEVEL;
            this.MESSAGE = MESSAGE;
        }

        private final Integer LEVEL;
        private final String MESSAGE;

        public static CategoryLevel getCategoryLevelEnum(Integer type){
            for (CategoryLevel level:CategoryLevel.values()){
                if (level.LEVEL.intValue()==type.intValue()){
                    return level;
                }
            }
            return null;
        }
    }
}

